<?php include("common/conn.php"); 

// 查询goodselect表数据
if (!empty($_REQUEST[ziid])) {
	$query = mysql_query("select * from select_model where ziid = ".$_REQUEST[ziid]);
	while ($row = mysql_fetch_assoc($query)) {
		$select_model[] = $row;
	}
	foreach ($select_model as $k => $v) {
		$select[$k][name] = $v[fTitle];
		if ($v[sTitle]!='') {
			$select[$k][classs][name] = $v[sTitle];
			$select[$k][classs][num] = $v[number];
			$select[$k][classs][price] = $v[price];				
		}else{
			$select[$k][num] = $v[number];
			$select[$k][price] = $v[price];	
		}
	}
	$fTitle = array();
	foreach ($select as $k => $v) {
		$fTitle[$k] = $v[name];
	}
	$fTitle_key = array();
	foreach ($fTitle as $k => $v) {
		$fTitle_key[$v][] = $k;
	}
	$data = array();
	foreach ($fTitle_key as $k => $v) {
		foreach ($select as $key => $value) {
			if ($k == $value[name]) {
				$data[$k][name] = $value[name];
				if ($value[classs]!='') {
					$data[$k][classs][] = $value[classs];
				}else{
					$data[$k][num] = $value[num];
					$data[$k][price] = $value[price];
					$data[$k][classs] = array();
				}
			}
		}
	}
}else{
	$data = array();
}
// 查询goodselect表数据 end

if($_GET['gid']!=""){
	$rert30=mysql_query("select * from goods where goodsid='".$_GET['gid']."'");
	$sdg30=mysql_fetch_array($rert30);
	// 判断该商品是否有配送时间
	$query = mysql_query("select * from zifenlei where id=".$sdg30[zixilie]);
	$zixilie = mysql_fetch_assoc($query);
	$hasDeliveryTime = $zixilie[type];
	// 判断该商品是否有配送时间 end
}
$citysql=mysql_query("select * from city");
if ($_REQUEST[act]=='selectcat') {
	$query1=mysql_query("select * from zifenlei where fenleiid=(select leimu from shop where shopid='".$_REQUEST[shopid]."')");
	$arr = array();
	$i=0;
	if ($_REQUEST[gid]!='') {
		$query=mysql_query("select * from goods where goodsid='".$_REQUEST[gid]."'");
		$goods=mysql_fetch_array($query);
		while ($row=mysql_fetch_array($query1)) {
			$arr[$i][id]=$row[id];
			$arr[$i][name]=$row[name];
			if ($goods[zixilie]==$row[id]) {
				$arr[$i][select]='true';
			}else{
				$arr[$i][select]='false';
			}
			$i++;
		}
	}else{
		while ($row=mysql_fetch_array($query1)) {
			$arr[$i][id]=$row[id];
			$arr[$i][name]=$row[name];
			$i++;
		}
	}


	echo json_encode($arr);
	exit();
}
// 添加规格
if (!empty($_GET[data])) {
	$data = json_decode($_GET[data],true);
	$query = mysql_query("DELETE from select_model where ziid = ".$data[0][ziid]);
	$fSum = count($data);
	for ($i=0; $i < $fSum; $i++) {
		if (isset($data[$i][classs])) {
			$classs = $data[$i][classs];
			$sSum = count($classs);
			for ($j=0; $j < $sSum; $j++) { 
				$query=mysql_query("INSERT INTO select_model (ziid,fTitle,sTitle,price,`number`) VALUES ('".$data[$i][ziid]."','".$data[$i][name]."','".$classs[$j][name]."','".$classs[$j][price]."','".$classs[$j][num]."')");
			}
		}else{
			$query=mysql_query("INSERT INTO select_model (ziid,fTitle,price,`number`) VALUES ('".$data[$i][ziid]."','".$data[$i][name]."','".$data[$i][price]."','".$data[$i][num]."')");
		}
	}
	// echo "<script>alert('添加成功');window.location.href='addzifen.php?id=".$data[0][id]."'</script>";
	exit;
}
// 添加规格 end
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link href="css/styles.css" type="text/css"/>
	<title>e网批发管理后台</title>
	<link href="css/styles.css" rel="stylesheet" type="text/css" />
	<link href="css/style2.css" rel="stylesheet" type="text/css" />
	<style type="text/css">
		.cy_mode{position: relative;left:0px; width:100%;  text-align:left; line-height:30px; margin-top:5px; border:0px;   overflow:hidden; padding-bottom:10px;}
		.indexfloat{ position:relative;float:left; margin-left:0px; margin-top:0px;}
		.indexfloat input{
			margin-top: 20px;
		}
		.zi td {
			position:relative;
			left:220px;
		}
		ol,
		ul {
			list-style: none;
		}

		#class>li {
			padding: 5px 10px;
			border-bottom: 1px solid #999
		}

		#class>li ul {
			margin: 10px 80px;
		}

		#class>li span {
			margin: 0 10px;
		}

		#class >li ul li {
			line-height: 40px;
		}
	</style>
	<?php 

	if($_GET['action']=="ok")
	{ 

		if($_GET[shopid]!="")
		{
			$shopid=$_GET[shopid];
		}
		else
		{
			$shopid=$_POST[shopid];
		}
		$rert31=mysql_query("select *from shop where shopid='".$shopid."'");
		$rowf1=mysql_fetch_array($rert31);
		$xilie=$rowf1[leimu];
		$srat=mysql_query("INSERT INTO goods(title,shangjia,price,content,summary,xilie,zixilie,yuanjia,xihuan,city,longitude,latitude,shopid,maxage,minage,number,leimu,delivery_time) VALUES ('$_REQUEST[title]','$_REQUEST[shangjia]','$_REQUEST[price]','$_REQUEST[content]','$_REQUEST[summary]','$xilie','$_REQUEST[zixilie]','$_REQUEST[yuanjia]','$_REQUEST[xihuan]','$_POST[city]','$rowf1[longitude]','$rowf1[latitude]','$shopid','$_REQUEST[maxage]','$_REQUEST[minage]','$_REQUEST[num]','$_REQUEST[leimu]','$_REQUEST[delivery_time]')");
		$g=mysql_insert_id();
		$query = mysql_query("UPDATE select_model set goodsid = ".$g." where goodsid = 0");
		$srat=mysql_query("INSERT INTO coupon(goodsid,shopid) VALUES ('$g','$shopid')");
		echo "<script>window.location.href='goods2.php?shopid=$_GET[shopid]';</script>";
	}
	if($_GET['ch']=="ok")
	{    
		if($_GET[shopid]!="")
		{
			$shopid=$_GET[shopid];
		}
		else
		{
			$shopid=$_POST[shopid];
		}
		$rert34=mysql_query("select *from shop where shopid='".$shopid."'");
		$rowf4=mysql_fetch_array($rert34);
		$xilie=$rowf4[leimu];
		// $content = addslashes($_POST['content']);
		$srat1=mysql_query("update goods set title='".$_POST['title']."',shangjia='".$_POST['shangjia']."',price='".$_POST['price']."',content='".$_POST['content']."',shopid='".$_POST['shopid']."',yuanjia='".$_POST['yuanjia']."',city='".$_POST['city']."',xilie='".$xilie."',zixilie='".$_POST['zixilie']."',summary='".$_POST['summary']."',longitude='".$_POST['longitude']."',latitude='".$_POST['latitude']."',xihuan='".$_POST['xihuan']."',minage='".$_POST['minage']."',maxage='".$_POST['maxage']."',number='".$_POST['num']."',leimu='".$_POST['leimu']."',delivery_time='".$_POST['delivery_time']."' where goodsid='".$_REQUEST['gid']."'");

		echo "<script>window.location.href='goods2.php?shopid=$_GET[shopid]';</script>";
	}
	if($_GET[del]=="ok")
	{
		$srat1=mysql_query("DELETE FROM select_model where selectid='".$_REQUEST['gid']."'");
	}


	?>
	<script type="text/javascript" src="./js/jquery.js"></script>
	<script type="text/javascript" src="js/jquery.validate.js"></script>
	<script type="text/javascript" src="js/addgoods.js"></script>
	<script>

		var maxstrlen=1500;

		function Q(s){return document.getElementById(s);}

		function checkWord(c){

			len=maxstrlen;

			var str = c.value;

			myLen=getStrleng(str);

			var wck=Q("wordCheck");

			if(myLen>len*2){

				c.value=str.substring(0,i+1);

			}

			else{

				wck.innerHTML = Math.floor((len*2-myLen)/2);

			}

		}

		function getStrleng(str){

			myLen =0;

			i=0;

			for(;(i<str.length)&&(myLen<=maxstrlen*2);i++){

				if(str.charCodeAt(i)>0&&str.charCodeAt(i)<128)

					myLen++;

				else

					myLen+=2;

			}

			return myLen;

		}



	</script>
</head>

<body>

	<!--header-->
	<?php include("common/header.php");?>
	<!--header end-->
	<!-- menu  -->
	<div id="sub_nav">
		<div id="subnav_wrap" class="container_12">
			<div id="subnav" class=" grid_12">
				<ul>
					<li> <a href="fenlei.php" class="sub_nav_active"> 分类管理 </a></li>
					<li><a href="fenlei_leixing.php">分类属性</a></li>
				</ul>
			</div>
		</div>
	</div>
	<!-- menu end -->
	<!--规格-->
	<div style="border: 1px solid gray;width: 80%;margin-left: 10%;">
		<div class="car_mode">
			<div class="indexfloat" style="width:15%; margin-left:5%;">
				规格
			</div>
			<div class="indexfloat" style="width:79%;">
				<ul id="class">
					<?php foreach ($data as $v) { ?>
					<li>
						名字:
						<input type="text" value="<?php echo $v[name]; ?>">
						<span <?php if (!empty($v[classs])) { ?>
							style="display:none;" <?php } ?>>
							价格:
							<input type="text" value="<?php if (!empty($v[price])) {echo $v[price];} ?>">
							数量：
							<input type="text" value="<?php if (!empty($v[num])) {echo $v[num];}?>">
						</span>
						<button onclick="ad_class(this)">添加子类</button>
						<button onclick="remove_min_class(this)">删除</button>
						<ul>
							<?php foreach ($v[classs] as $value) { ?>
							<li >
								子类:
								<input type="text" value="<?php echo $value[name]; ?>"> 价格:
								<input type="text" value="<?php echo $value[price]; ?>"> 数量:
								<input type="text" value="<?php echo $value[num]; ?>">
								<button onclick="remove_class(this)">删除</button>
							</li>
							<?php } ?>
						</ul>
					</li>
					<?php } ?>
				</ul>

				<div class="class_html" style="display: none ;">

					<li id="min-class">
						名字:<input type="text">
						<span>
							价格:<input type="text">
							数量：<input type="text">
						</span>
						<button>添加子类</button>
						<button onclick="remove_min_class(this)">删除</button>
						<ul></ul>
					</li>
					<li id="m-class">
						子类:
						<input type="text"> 价格:
						<input type="text"> 数量:
						<input type="text">
						<button onclick="remove_class(this)">删除</button>
					</li>
					<input type="hidden" id="ziid" value="<?php echo $_GET[ziid]; ?>">
					<input type="hidden" id="id" value="<?php echo $_GET[id]; ?>">
				</div>
				<button onclick="add_main_class()">添加</button>
			</div>
		</div>
	</div>
	<!--规格 end-->	

	<div class="submit_div">		
		<input name="" type="submit" value="提交" onclick="guige_submits();" id="submit" />
	</div>

</body>
</html>

<script type="text/javascript">
	// 表单验证
	$().ready(function() {
		$("#form").validate();
	});
	// 表单验证 end
	function zi(n)
	{
		for(var i=1;i<=30;i++)
		{
			document.getElementById(i).style.display="none"; 
			document.getElementById(n).style.display="block";  
		}

	}

</script>
<script type="text/javascript">


	function shop(m)   
	{
		for(var j=31;j<=60;j++)
		{
			document.getElementById(j).style.display="none"; 
			document.getElementById(m).style.display="block";  
		}
	}


	var shopid=$('.shopid');
	var gid=$('#gid');
	shopid.change(function(){
		$('.zixilie').empty();
		var shopidval=shopid.val();
		var gidval=gid.val();
		var url='addgoods.php?act=selectcat&shopid='+shopidval+'&gid='+gidval;
		
		$.get(url,function(data){
			var infos = JSON.parse(data);

			for(var i=0;i<infos.length;i++){
				$('.zixilie').append("<option  value='"+infos[i].id+"'>"+infos[i].name+"</option>")
			};
		});
	});
	// 提交表单
	
	$('#submit').click(function(){
		editor.sync(); 
		$('#form').submit();
	})
	// 提交表单 end
	// 根据子分类查询子分类类型(海淘或快购),若为快购,需要设置配送时间
	$('select[name="zixilie"]').change(function(){
		var zixilie = $(this).val();
		var url = 'addgoods.php?act=getType&zixilie='+zixilie;
		$.get(url,function(data){
			if (data == 1) {
				$('.delivery_time').show();
			}else{
				$('.delivery_time').hide();
			};
		})
	})
	// 根据子分类查询子分类类型(海淘或快购),若为快购,需要设置配送时间 end
	// 根据商家查询子分类类型(海淘或快购),若为快购,需要设置配送时间
	$('select[name="shopid"]').change(function(){
		var shopid = $(this).val();
		var url = 'addgoods.php?act=getType&shopid='+shopid;
		$.get(url,function(data){
			if (data == 1) {
				$('.delivery_time').show();
			}else{
				$('.delivery_time').hide();
			};
		})
	})
	// 根据商家查询子分类类型(海淘或快购),若为快购,需要设置配送时间
</script>